#!/usr/bin/bash

set -e
cli_build=n
gui_build=n
soft="$0"
help() {
        echo -e "Usage:\n" \
            "$soft cli\n" \
            "$soft gui\n" \
            "$soft"
        exit 0
}
if [ -n "$1" ]; then
        [ -n "$2" ] && help
        case "$1" in
        cli)
                cli_build=y
        ;;
        gui)
                gui_build=y
        ;;
        *)
                help
        ;;
        esac
else
        cli_build=y
        gui_build=y
fi

set -ex
EXE="./diskTest"
mkdir -p build/

for i in pot/*; do
        lang="${i##*/}"
        lang="${lang%.*}"
        mkdir -p build/locale "build/locale/$lang/LC_MESSAGES"
        msginit -i "$i" -o "build/locale/$lang.po" -l "$lang.UTF-8" --no-translator
        msgfmt "build/locale/$lang.po" -o "build/locale/$lang/LC_MESSAGES/diskTest.mo"
done

if [ "y" = "$cli_build" ]; then
        cd src
        gcc -Wpedantic -Wall -Wextra -march=native *.c -o "../build/$EXE" \
            -g -O3 -std=c23 -D_GNU_SOURCE \
            -lz
        cd -
fi
if [ "y" = "$gui_build" ]; then
        cd src
        gcc -Wpedantic -Wall -Wextra -march=native *.c -o "../build/${EXE}_ui" \
            -g3 -O3 -std=c23 -D_GNU_SOURCE \
            -D_GUI $(pkg-config --cflags --libs gtk+-3.0)
        cp ui.glade ../build/
        cd -
fi